package cn.iocoder.yudao.module.member.controller.admin.praxisProjectType;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortingField;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.member.controller.admin.newsStudyType.vo.NewsStudyTypeIdReqVO;
import cn.iocoder.yudao.module.member.controller.admin.praxisProjectType.vo.*;
import cn.iocoder.yudao.module.member.convert.praxisProjectType.PraxisProjectTypeConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.praxisProjectType.PraxisProjectTypeDO;
import cn.iocoder.yudao.module.member.service.praxisProjectType.PraxisProjectTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 实践项目类型")
@RestController
@RequestMapping("/member/praxis-type")
@Validated
public class PraxisProjectTypeController {

    @Resource
    private PraxisProjectTypeService praxisProjectTypeService;

    @PostMapping("/create")
    @ApiOperation("创建实践项目类型")
    public CommonResult<Long> createPraxisProjectType(@Valid @RequestBody PraxisProjectTypeCreateReqVO createReqVO) {
        return success(praxisProjectTypeService.createPraxisProjectType(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新实践项目类型")
    public CommonResult<Boolean> updatePraxisProjectType(@Valid @RequestBody PraxisProjectTypeUpdateReqVO updateReqVO) {
        praxisProjectTypeService.updatePraxisProjectType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除实践项目类型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deletePraxisProjectType(@RequestParam("id") Long id) {
        praxisProjectTypeService.deletePraxisProjectType(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得实践项目类型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<PraxisProjectTypeRespVO> getPraxisProjectType(@RequestParam("id") Long id) {
        PraxisProjectTypeDO praxisProjectType = praxisProjectTypeService.getPraxisProjectType(id);
        return success(PraxisProjectTypeConvert.INSTANCE.convert(praxisProjectType));
    }

    @GetMapping("/list")
    @ApiOperation("获得实践项目类型列表")
    public CommonResult<List<PraxisProjectTypeRespVO>> getPraxisProjectTypeList(  ) {
        PraxisProjectTypeExportReqVO exportReqVO=new PraxisProjectTypeExportReqVO();
        exportReqVO.setStatus(1);
        List<PraxisProjectTypeDO> list = praxisProjectTypeService.getPraxisProjectTypeList(exportReqVO);
        return success(PraxisProjectTypeConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/page")
    @ApiOperation("获得实践项目类型分页")
    public CommonResult<PageResult<PraxisProjectTypeRespVO>> getPraxisProjectTypePage(@Valid @RequestBody PraxisProjectTypePageReqVO pageVO) {
        SortingField sortingField = new SortingField();
        sortingField.setField("weight");
        sortingField.setOrder("desc");
        pageVO.setSortField(sortingField);
        PageResult<PraxisProjectTypeDO> pageResult = praxisProjectTypeService.getPraxisProjectTypePage(pageVO);
        return success(PraxisProjectTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出实践项目类型 Excel")
    @OperateLog(type = EXPORT)
    public void exportPraxisProjectTypeExcel(@Valid PraxisProjectTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PraxisProjectTypeDO> list = praxisProjectTypeService.getPraxisProjectTypeList(exportReqVO);
        // 导出 Excel
        List<PraxisProjectTypeExcelVO> datas = PraxisProjectTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "实践项目类型.xls", "数据", PraxisProjectTypeExcelVO.class, datas);
    }
    @PutMapping("/updateProjectTypeSort")
    @ApiOperation("项目类型排序")
    @TenantIgnore
    public CommonResult<Boolean> updateProjectTypeSort(@Valid @RequestBody NewsStudyTypeIdReqVO updateReqVO ) {
        praxisProjectTypeService.updateProjectTypeSort(updateReqVO.getIds());
        return success(true);
    }
}
